﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FreightSoft.Models;

namespace FreightSoft.Controllers
{
    public class ShipmentScheduleController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /ShipmentSchedule/

        public ActionResult Index()
        {

            return View(db.SHIPMENTSCHEDULEs.ToList());
        }

        //
        // GET: /ShipmentSchedule/Details/5

        public ActionResult Details(decimal id )
        {
            SHIPMENTSCHEDULE shipmentschedule = db.SHIPMENTSCHEDULEs.Find(id);
            PORT Org_port = db.PORTs.Find(shipmentschedule.ORIGINPORT);
            PORT dest_port = db.PORTs.Find(shipmentschedule.DESTINATIONPORT);
            ROUTE route = db.ROUTEs.Find(shipmentschedule.ROUTEID);      
      
            shipmentschedule.ORIGINPORT = Org_port.PORT_NAME.ToString();
            shipmentschedule.DESTINATIONPORT = dest_port.PORT_NAME.ToString();
            shipmentschedule.ROUTEID = route.NAME.ToString();
            if (shipmentschedule == null)
            {
                return HttpNotFound();
            }
            return View(shipmentschedule);
        }

        //
        // GET: /ShipmentSchedule/Create

        public ActionResult Create()
        {

            var port_list = db.PORTs.ToList();
            foreach (var i in port_list)
            {
                i.PORT_ID = i.PORT_ID.Trim();
            }
            var PortList = port_list;
            var dropdownList = new SelectList(PortList, "Port_Id", "Port_Name");
            ViewData["Org_Port_ID"] = dropdownList;
            ViewData["Dest_Port_ID"] = dropdownList;

            var ship_list = db.SHIPs.ToList();
            foreach (var i in ship_list)
            {
                i.SHIPID = i.SHIPID.Trim();
            }
            var shiplist = ship_list;
            var shipdropdownlist = new SelectList(shiplist, "ShipId", "S1");
            ViewData["Ship_Id"] = shipdropdownlist;

            var route_list = db.ROUTEs.ToList();
            foreach (var i in route_list)
            {
                i.ROUTEID = i.ROUTEID.Trim();
            }
            var routeList = route_list;
            var routedropdownlist = new SelectList(routeList, "RouteId", "Name");
            ViewData["Route_Id"] = routedropdownlist;

            ViewBag.SHIPID = new SelectList(db.SHIPs, "Ship_Id", "S1");
            ViewBag.ORIGINPORT = new SelectList(db.PORTs, "Org_Port_Id", "Port_Name");
            ViewBag.DESTINATIONPORT = new SelectList(db.PORTs, "Dest_Port_Id", "Port_Name");
            ViewBag.ROUEID = new SelectList(db.ROUTEs, "RouteId", "Name");


            return View();

        }

        //
        // POST: /ShipmentSchedule/Create

        [HttpPost]
        public ActionResult Create(SHIPMENTSCHEDULE shipmentschedule, FormCollection collection )
        {
            if (ModelState.IsValid)
            {
                
                shipmentschedule.ORIGINPORT = collection.Get("selectedOriginPortId");
                shipmentschedule.DESTINATIONPORT = collection.Get("selectedDestPortId");
                shipmentschedule.SHIPID = collection.Get("selectedShipId");
                shipmentschedule.ROUTEID = collection.Get("selectedRouteId");
                var idd = db.SHIPMENTSCHEDULEs.Count();
                var my1 = idd + 1001;

                shipmentschedule.SCHEDULEID = my1;
                shipmentschedule.STATUS = "Planned";
                db.SHIPMENTSCHEDULEs.Add(shipmentschedule);
                db.SaveChanges();

                //return View(shipmentschedule);
                //return RedirectToAction("ReservationSummery", "Reservation", new { HotelName = name, CustomerId = custId, FirstName = customer.FirstName });
                return RedirectToAction("Details", new { Id = shipmentschedule.SCHEDULEID });
            }

            return View(shipmentschedule);
        }

        //
        // GET: /ShipmentSchedule/Edit/5

        public ActionResult Edit(decimal id = 0)
        {
            SHIPMENTSCHEDULE shipmentschedule = db.SHIPMENTSCHEDULEs.Find(id);
            if (shipmentschedule == null)
            {
                return HttpNotFound();
            }
            return View(shipmentschedule);
        }

        //
        // POST: /ShipmentSchedule/Edit/5

        [HttpPost]
        public ActionResult Edit(SHIPMENTSCHEDULE shipmentschedule)
        {
            if (ModelState.IsValid)
            {
                db.Entry(shipmentschedule).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(shipmentschedule);
        }

        //
        // GET: /ShipmentSchedule/Delete/5

        public ActionResult Delete(decimal id = 0)
        {
            SHIPMENTSCHEDULE shipmentschedule = db.SHIPMENTSCHEDULEs.Find(id);
            if (shipmentschedule == null)
            {
                return HttpNotFound();
            }
            return View(shipmentschedule);
        }

        //
        // POST: /ShipmentSchedule/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {
            SHIPMENTSCHEDULE shipmentschedule = db.SHIPMENTSCHEDULEs.Find(id);
            db.SHIPMENTSCHEDULEs.Remove(shipmentschedule);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult AddShipment(decimal scheduleId, string route_id, string origin_port, string destination_port)
        {
            List<SHIPMENT> shipments = GetShipmentsForTransport(route_id);
            return View(shipments.ToList());
            //return RedirectToAction("Index", "Home");

        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        // get shipments that are ready for shipment - from given place to another 
        protected List<SHIPMENT> GetShipmentsForTransport(string route_id)
        {
            //var port_list = from route in db.ROUTEs
            //                where (route.ROUTE_ID == route_id)
            //                select route;
            var records = from shipments in db.SHIPMENTs
                          //where (shipments.ORIGINPORT == origin_port) && (shipments.DESTINATIONPORT == destination_port)
                          //&& (shipments.STATUS == "Released")
                          select shipments;

            return records.ToList();
        }

    }
}